<?php
/**
 * Created by PhpStorm.
 * User: khanhht
 * Date: 7/15/14
 * Time: 2:11 PM
 */

class My_Service_Game extends My_Service_Abstract {


    public function __construct(){
        $this->_model = new Default_Model_Game();
        parent::__construct();
    }

    /**
     * Get all games of a user
     * @param $userId
     * @return array|mixed
     */

    public function getGameIdByUser($userId){
        $key = sprintf($this->_cache_key,md5(__CLASS__ . __FUNCTION__ . json_encode(func_get_args())));
        $result = $this->_memcache->get($key);
        if(!$result){
            $result = array();
            $data = $this->_model->getDataByUser($userId)->toArray();
            if(!$data){
                return null;
            }else{
                /* if user has multi roles */
                if(count($data)>1){
                    foreach($data as $row){
                        $result += json_decode($row['gameId']);
                    }
                    $result = array_unique($result);
                }else{
                    $data = current($data);
                    $result = json_decode($data['gameId']);
                }
                $this->_memcache->set($key,$result,false,0);
            }

        }
        return $result;
    }
    public function getGamesByUser($userId){
        $key = sprintf($this->_cache_key,md5(__CLASS__ . __FUNCTION__ . json_encode(func_get_args())));
        $result = $this->_memcache->get($key);
        if(!$result){
            $result = array();
            $data = $this->_model->getDataByUser($userId)->toArray();
            if(!$data){
                return null;
            }else{
                /* if user has multi roles */
                if(count($data)>1){
                    foreach($data as $row){
                        $result += json_decode($row['gameId']);
                    }
                    $result = array_unique($result);
                }else{
                    $data = current($data);
                    $result = json_decode($data['gameId']);
                }
                $result = $this->_model->getDataByGameList($result);
                $this->_memcache->set($key,$result,false,0);
            }
        }
        
        return $result;
    }






} 